<template>
  <div v-loading="loading">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="120px"
      class="dataForm"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('公司全称')" prop="company.companyName">
            <!-- <el-input v-model="form.companyFullName" :placeholder="$t('公司全称')"  /> -->
            <el-autocomplete
              v-model="form.company.companyName"
              class="inline-input"
              :fetch-suggestions="companyQuerySearch"
              placeholder="请输入公司名"
              :trigger-on-focus="false"
              :maxlength="100"
              show-word-limit
              @select="companyHandleSelect"
            >
              <i slot="prefix" class="el-input__icon el-icon-search" />
              <template
                v-if="form.id"
                slot="append"
                @click="companyHandleSelect"
              >
                <router-link
                  target="_blank"
                  :to="{
                    path: '/company/base/companyBaseHome',
                    query: { companyId: form.companyId },
                  }"
                  class="link_a"
                >{{ $t("查看标的公司信息") }}
                </router-link>
              </template>
            </el-autocomplete>
          </el-form-item>
        </el-col>
        <!--        <el-col :span="12">-->
        <!--          <el-form-item :label="$t('公司分类')" prop="companyClass">-->
        <!--            <dictSelect-->
        <!--              v-model="form.companyClass"-->
        <!--              :clearable="true"-->
        <!--              dictionary="company_class"-->
        <!--            />-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :span="12">
          <el-form-item :label="$t('公司简称')" prop="company.companyShortName">
            <el-input
              v-model="form.company.companyShortName"
              :placeholder="$t('公司简称')"
              show-word-limit
              :maxlength="100"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('企业统一社会信用代码')" prop="company.creditCode">
            <el-input
              v-model="form.company.creditCode"
              :placeholder="$t('企业统一社会信用代码')"
              show-word-limit
              :maxlength="100"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('法人代表')" prop="company.legalPerson">
            <el-input
              v-model="form.company.legalPerson"
              :placeholder="$t('法人代表')"
              show-word-limit
              :maxlength="100"
            />
          </el-form-item>
        </el-col>
        <!--行业-->
        <Industry ref="industryZj" :buss-data="form.company" />
        <!--地域-->
        <Area ref="cityZj" :buss-data="form.company" />
        <el-col :span="24">
          <el-form-item :label="$t('注册详细地址')" prop="company.detailsRegistered">
            <el-input
              v-model="form.company.detailsRegistered"
              :placeholder="$t('注册详细地址')"
              show-word-limit
              maxlength="500"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('注册资本') + $t('金额单位')"
            prop="company.capitalRegistered"
          >
            <Money
              v-model="form.company.capitalRegistered"
              unit="w"
              :placeholder="$t('注册资本')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('注册资本币种')" prop="company.currency">
            <el-select
              v-model="form.company.currency"
              filterable
              :placeholder="$t('注册资本币种')"
            >
              <el-option
                v-for="dict in currencyOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('实收资本') + $t('金额单位')"
            prop="company.actualCapital"
          >
            <Money
              v-model="form.company.actualCapital"
              unit="w"
              :placeholder="$t('实收资本')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="$t('实收资本币种')"
            prop="company.actualCurrency"
          >
            <el-select
              v-model="form.company.actualCurrency"
              filterable
              :placeholder="$t('实收资本币种')"
            >
              <el-option
                v-for="dict in actualCurrencyOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('成立时间')" prop="setDate">
            <DatePicker
              v-model="form.company.setDate"
              :placeholder="$t('成立时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('上市情况')" prop="listingSituation">
            <dict-select
              v-model="form.company.listedSituation"
              dictionary="bf_group_listed_situation"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('上市板块')" prop="listedSector">
            <dict-select
              v-model="form.company.listedSector"
              dictionary="bf_group_listed_sector"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('股票代码')" prop="stockCode">
            <el-input
              v-model="form.company.stockCode"
              placeholder="股票代码"
              show-word-limit
              maxlength="100"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.id" :label="$t('创建人')" prop="createBy.id">
            <UserSelect v-model="form.createBy.id" :placeholder="$t('创建人')" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.id" :label="$t('更新时间')" prop="updateTime">
            <el-input
              v-model="form.updateTime"
              disabled
              show-word-limit
              maxlength="50"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('经营范围')" prop="businessScope">
            <el-input
              v-model="form.company.businessScope"
              type="textarea"
              :placeholder="$t('经营范围')"
              show-word-limit
              maxlength="2000"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('企业简介')" prop="remark">
            <el-input
              v-model="form.company.remark"
              type="textarea"
              :placeholder="$t('企业简介')"
              show-word-limit
              maxlength="2000"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right">
      <!--保存 取消按钮-->
      <save-btn has-permi="system:company" />
    </div>
  </div>
</template>

<script>
import {
  saveDealCompany,
  getDealCompany,
  companyDetailsByName
} from '@/api/deal/estate/company/dealCompany'
import {
  companyListByName
} from '@/api/company/base/companyBase'
// 行业组件
import Industry from '@/components/Industry'
// 省市组件
import Area from '@/components/Area'
export default {
  components: {
    Industry,
    Area
  },
  data() {
    return {
      // 表单参数
      form: {
        company: {}
      },
      // 注册资本币种字典
      currencyOptions: [],
      // 是否上市
      yesOrNoOptions: [],
      actualCurrencyOptions: [],
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        companyClass: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        // industryFirst: [
        //   { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        // ],
        // industrySecond: [
        //   { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        // ],
        //  'provinceRegistered': [
        //   { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        // ],
        // 'cityRegistered': [
        //   { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        // ],
        'capitalRegistered': [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        'company.currency': [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        // 'company.actualCapital': [
        //   { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        // ],
        // 'company.actualCurrency': [
        //   { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        // ],
        'company.companyName': [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        'company.companyShortName': [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        'company.capitalRegistered': [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        'company.legalPerson': [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        'company.detailsRegistered': [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    this.getDicts('currency').then((response) => {
      this.currencyOptions = response.data
      this.actualCurrencyOptions = response.data
    })
    this.getDicts('sys_yes_no').then((response) => {
      this.yesOrNoOptions = response.data
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      // this.loading = true
      if (data != null && data.id !== undefined) {
        getDealCompany(data.id).then((response) => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: this.$route.query.id,
        companyClass: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        company: {
          companyName: '',
          companyFullName: undefined,
          companyShortName: undefined,
          creditCode: undefined,
          legalPerson: undefined,
          setDate: undefined,
          industryFirst: undefined,
          industrySecond: undefined,
          provinceRegistered: undefined,
          cityRegistered: undefined,
          detailsRegistered: undefined,
          capitalRegistered: undefined,
          currency: undefined,
          businessScope: undefined,
          actualCapital: undefined,
          actualCurrency: undefined,
          stockCode: undefined,
          listingAddress: undefined,
          market: undefined,
          logoInfo: undefined,
          tycId: undefined,
          remark: undefined
        }
      }
      this.resetForm('form')
      this.$nextTick(() => {
        this.$refs.industryZj.reset()
        this.$refs.cityZj.reset()
      })
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.company.tycId == null) {
            return this.msgError('未查到该公司')
          }
          this.loading = true
          saveDealCompany(this.form).then((response) => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    // 标的公司查询
    companyQuerySearch(queryString, cb) {
      companyListByName(queryString).then((res) => {
        if (res.code === 200) {
          cb(res.data)
        }
      })
    },
    // 公司选择后事件
    companyHandleSelect(item) {
      const company = {
        companyName: item.value,
        tycId: item.id,
        orgType: item.type
      }
      this.loading = true
      // 根据天眼id 公司名查询公司详细信息
      companyDetailsByName(company)
        .then((res) => {
          if (res.data != null && res.msg !== 'exist') {
            const data = res.data
            // this.form.company=data;
            this.form.company.businessScope = data.businessScope
            this.form.company.tycId = data.tycId
            this.form.company.capitalRegistered = data.capitalRegistered
            this.form.company.currency = data.currency
            this.form.company.industryFirst = data.industryFirst
            this.form.company.industrySecond = data.industrySecond
            this.form.company.creditCode = data.creditCode
            this.form.company.cityRegistered = data.cityRegistered
            this.form.company.provinceRegistered = data.provinceRegistered
            this.form.company.detailsRegistered = data.detailsRegistered
            this.form.company.legalPerson = data.legalPerson
            this.form.company.remark = data.remark
            this.form.company.setDate = data.setTime
            // this.form.company.capitalRegistered = data.registCapital
            this.form.company.companyShortName = data.companyShortName
            this.$nextTick(() => {
              this.$refs.industryZj.initIndustrySecond()
              this.$refs.cityZj.initCity()
            })
            this.$message({
              message: '获取工商信息成功',
              type: 'success'
            })
          } else if (res.msg === 'exist') {
            const data = res.data
            this.form.company = data
            this.$nextTick(() => {
              this.$refs.industryZj.initIndustrySecond()
              this.$refs.cityZj.initCity()
            })
            // this.$message('该公司已存在')
          }
          this.loading = false
        })
        .catch(function() {
          this.loading = false
        })
        this.loading = false
      // this.form.dealName = this.form.company.companyName;
      // this.form.company.tycId = item.id;
      // this.checkDealNameExist();
      // this.checkTycAfter(item);
    }
  }
}
</script>
